package me.learningandroid.ch2_2.intentdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import org.w3c.dom.Text;

public class IntentForResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_for_result);

        Button submitBtn = (Button) findViewById(R.id.submit_btn);
        Button cancelBtn = (Button) findViewById(R.id.cancel_btn);
        EditText resultEditText = (EditText) findViewById(R.id.edit_return_value);

        submitBtn.setOnClickListener((view)-> {
            Intent intent = new Intent();
            intent.putExtra("result", resultEditText.getText().toString());

            setResult(RESULT_OK, intent);
            finish();
        });
        cancelBtn.setOnClickListener((view)-> {
            setResult(RESULT_CANCELED, null);
            finish();
        });
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED, null);
        super.onBackPressed();
    }
}